package se.jocke.nb.log.ui;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;
import javax.swing.JComponent;
import org.netbeans.spi.options.OptionsPanelController;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import se.jocke.nb.log.LogViewFilterManager;
import se.jocke.nb.log.model.LogViewFilter;

@OptionsPanelController.TopLevelRegistration(
        categoryName = "#OptionsCategory_Name_LogView",
        iconBase = "se/jocke/nb/log/ui/text3765.png",
        keywords = "#OptionsCategory_Keywords_LogView",
        keywordsCategory = "LogView"
)
@org.openide.util.NbBundle.Messages({"OptionsCategory_Name_LogView=Log View", "OptionsCategory_Keywords_LogView=Log"})
public final class LogViewOptionsPanelController extends OptionsPanelController {

    private LogViewPanel panel;

    private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    private boolean changed;

    @Override
    public void update() {
        getPanel().load();
        changed = false;
    }

    @Override
    public void applyChanges() {
        getPanel().store();
        changed = false;
    }

    @Override
    public void cancel() {
        // need not do anything special, if no changes have been persisted yet
    }

    @Override
    public boolean isValid() {
        return getPanel().valid();
    }

    @Override
    public boolean isChanged() {
        return changed;
    }

    @Override
    public HelpCtx getHelpCtx() {
        return null; // new HelpCtx("...ID") if you have a help set
    }

    @Override
    public JComponent getComponent(Lookup masterLookup) {
        return getPanel();
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener l) {
        pcs.addPropertyChangeListener(l);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener l) {
        pcs.removePropertyChangeListener(l);
    }

    public List<LogViewFilter> loadLogViewFilters() {
        return Lookup.getDefault().lookup(LogViewFilterManager.class)
                .findAll();
    }

    public LogViewFilter findDefault() {
        for (LogViewFilter logViewFilter : loadLogViewFilters()) {
            if (logViewFilter.getName().equals(LogViewFilter.DEFAULT_FILTER_NAME)) {
                return logViewFilter;
            }
        }
        throw new IllegalStateException("Failed to find default filter");
    }

    public void storeLogViewFilters(List<LogViewFilter> filters) {
        Lookup.getDefault().lookup(LogViewFilterManager.class)
                .store(filters);
    }

    private LogViewPanel getPanel() {
        if (panel == null) {
            panel = new LogViewPanel(this);
        }
        return panel;
    }

    void changed() {
        if (!changed) {
            changed = true;
            pcs.firePropertyChange(OptionsPanelController.PROP_CHANGED, false, true);
        }
        pcs.firePropertyChange(OptionsPanelController.PROP_VALID, null, null);
    }

}
